<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../../static/assets/global/css/components.css">
    <link rel="stylesheet" type="text/css" href="../../static/assets/admin/pages/css/login.css">
    <script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
    <title>Title</title>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">商品详情</h3>
        <div id="promoDateContainer" class="form-group">
            <label style="color: blue;" class="control-label">活动</label>
            <div>
                <label style="color: red;" class="control-label" id="promoDate"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">商品名</label>
            <div>
                <label class="control-label" id="title"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">商品描述</label>
            <div>
                <label class="control-label" id="description"></label>
            </div>
        </div>
        <div id="normalPriceContainer" class="form-group">
            <label class="control-label">价格</label>
            <div>
                <label class="control-label" id="price"></label>
            </div>
        </div>
        <div id="promoPriceContainer" class="form-group">
            <label style="color: red;" class="control-label">秒杀价格</label>
            <div>
                <label style="color: red;" class="control-label" id="promoPrice"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">销量</label>
            <div>
                <label class="control-label" id="sales"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">库存</label>
            <div>
                <label class="control-label" id="stock"></label>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">图片</label>
            <div>
                <img style="width: 200px; height: auto" id="imgUrl">
            </div>
        </div>
        <div class="form-actions">
            <button class="btn blue" id="createOrder" type="submit">
                下单
            </button>
        </div>
    </div>
</body>
<script>
    var g_itemVo={};

    jQuery(document).ready(function(){
        $("#createOrder").on("click",function () {
            $.ajax({
                type:"POST",
                contextType:"application/x-www-form-urlencoded",
                url:"http://localhost:8080/order/create",
                xhrFields:{withCredentials: true},
                data:{
                    "itemId":g_itemVo.id,
                    "amount":1,
                    "promoId":g_itemVo.promoId
                },
                success:function(data){
                    if(data.status == "success"){
                        alert("下单成功");
                        window.location.reload();
                    }else{
                        alert("下单失败，原因为"+data.data.errMsg);
                        //用户未登陆，跳转到登陆页面
                        if(data.data.errCode == 20003){
                            window.location.href="../user/login.html";
                        }
                    }
                },
                error:function (data) {
                    alert("下单失败，原因是"+data.responseText);
                }
            });
        });
            $.ajax({
                type:"GET",
                url:"http://localhost:8080/item/get",
                xhrFields:{withCredentials: true},
                data:{
                    "id":getParam("id")
                },
                success:function(data){
                    if(data.status == "success"){
                        g_itemVo=data.data;
                        reloadDom();
                        //设置定时器，每隔1s刷新活动状态
                        setInterval(reloadDom,1000);
                    }else{
                        alert("获取信息失败，原因为"+data.data.errMsg);
                    }
                },
                error:function (data) {
                    alert("获取信息失败，原因是"+data.responseText);
                }
            });
    });

    function reloadDom() {
        $("#title").text(g_itemVo.title);
        $("#description").text(g_itemVo.description);
        $("#price").text(g_itemVo.price);
        $("#sales").text(g_itemVo.sales);
        $("#stock").text(g_itemVo.stock);
        $("#imgUrl").attr("src",g_itemVo.imgUrl);

        //秒杀活动
        if(g_itemVo.promoStatus == 1){
            //活动未开始，计算倒计时
            var startTime = g_itemVo.startDate.replace(new RegExp("-","gm"),"/");
            startTime = (new Date(startTime)).getTime();
            var nowTime = Date.parse(new Date());
            var countdown = (startTime - nowTime)/1000;

            $("#promoDate").text("***秒杀活动将于 "+g_itemVo.startDate+" 开始*** 倒计时： "+countdown+"秒");
            //界面不刷新，会一直处于未开始状态
            if(countdown <= 0){
                //活动开始
                g_itemVo.prormStatus = 2;
                reloadDom();
            }

            $("#promoPrice").text(g_itemVo.promoPrice);
            //不能下单
            $("#createOrder").attr("disabled",true);
        }
        else if(g_itemVo.promoStatus == 2){
            //活动进行中
            var endTime = g_itemVo.endDate.replace(new RegExp("-","gm"),"/");
            endTime = (new Date(endTime)).getTime();
            var nowTime = Date.parse(new Date());
            var countdown = (endTime - nowTime)/1000;

            $("#promoDate").text("***秒杀活动将于 "+g_itemVo.endDate+" 结束*** 倒计时： "+countdown+"秒");
            //界面不刷新，会一直处于进行状态
            if(countdown <= 0){
                //活动开始
                g_itemVo.prormStatus = 3;
                reloadDom();
            }
            $("#promoPrice").text(g_itemVo.promoPrice);
            $("#createOrder").attr("disabled",false);
            //将原价隐藏
            $("#normalPriceContainer").hide();
        }
        else{
            $("#promoPriceContainer").hide();
            $("#promoDateContainer").hide();
        }
    }

    /*URL解析方法，获取URL上的id*/
    function getParam(paramName) {
        paramValue = "", isFound = !1;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        }
        return paramValue == "" && (paramValue = null), paramValue
    }
</script>
</html>